<template>
  <div>
    <div id="searchForm">
      <el-form :inline="true" :model="searchForm" class="demo-form-inline" ref="searchForm">
        <el-form-item label="" prop="headteacher">
          <el-input v-model="searchForm.headteacher" placeholder="班主任"></el-input>
        </el-form-item>
        <el-form-item label="" prop="class_room">
          <el-input v-model="searchForm.class_room" placeholder="教室"></el-input>
        </el-form-item>
        <el-form-item label="" prop="class_state">
          <el-select v-model="searchForm.class_state" placeholder="请选择状态" style="width: 100%">
            <el-option label="正常" :value="1"></el-option>
            <el-option label="结课" :value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="searchClass">查询</el-button>
          <el-button type="primary" @click="resetSearchFormData('searchForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-button type="primary" plain size="small" @click="add">新增</el-button>
    <el-table
      :data="tableData"
      style="width: 100%">
      <el-table-column
        prop="id"
        label="ID">
      </el-table-column>
      <el-table-column
        prop="class_name"
        label="班级名">
      </el-table-column>
      <el-table-column
        prop="class_room"
        label="教室">
      </el-table-column>
      <el-table-column
        prop="student_number"
        label="人数">
      </el-table-column>
      <el-table-column
        prop="headteacher"
        label="班主任">
      </el-table-column>
      <el-table-column
        prop="class_start"
        label="开班时间">
      </el-table-column>
      <el-table-column
        prop="class_state"
        label="状态">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.class_state"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-text="正常"
            inactive-text="结课"
            @change="changeState(scope.row.id,scope.row.class_state)"
            :active-value="1"
            :inactive-value="2">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.row)">编辑
          </el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="pageSizes"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    <el-dialog :title="title" :visible.sync="dialogFormVisible" @close="clearInfo">
      <el-form :model="form" :rules="rules" ref="dialogForm">
        <el-form-item label="班级名" :label-width="formLabelWidth" prop="class_name">
          <el-input v-model="form.class_name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="教室" :label-width="formLabelWidth" prop="class_room">
          <el-input v-model="form.class_room" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="人数" :label-width="formLabelWidth" prop="student_number">
          <el-input v-model="form.student_number" type="number" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth" prop="class_state">
          <el-select v-model="form.class_state" placeholder="请选择状态" style="width: 100%">
            <el-option label="正常" :value="1"></el-option>
            <el-option label="结课" :value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="班主任" :label-width="formLabelWidth" prop="headteacher">
          <el-input v-model="form.headteacher" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="开班时间" :label-width="formLabelWidth" prop="class_start">
          <el-date-picker style="width: 100%"
                          v-model="form.class_start"
                          type="date"
                          format='yyyy-MM-dd'
                          value-format="yyyy-MM-dd"
                          placeholder="选择开班日期">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addOrUpDateClazz">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>

import {changeClazzState, clazzAddOrUpdate, clazzDelete, clazzOne, clazzPage} from "@/api/clazz";

export default {
  name: 'index',
  data() {
    return {
      title: '新增',
      tableData: [],
      currentPage: 1,
      pageSizes: [5, 10, 15, 20],
      pageSize: 5,
      total: 0,
      rules: {
        class_name: [
          {required: true, message: '请输入班级名称', trigger: 'blur'},
        ],
        class_room: [
          {required: true, message: '请输入教室名', trigger: 'blur'}
        ],
        class_state: [
          {required: true, message: '请选择班级状态', trigger: 'blur'}
        ],
        headteacher: [
          {required: true, message: '请输入班主任', trigger: 'blur'}
        ],
        student_number: [
          {required: true, message: '请输入班级人数', trigger: 'blur'}
        ],
        class_start: [
          {required: true, message: '请输入开班时间', trigger: 'blur'}
        ]
      },
      formLabelWidth: '80px',
      dialogFormVisible: false,  //  控制对话框的显示隐藏
      form: {
        class_name: '',
        class_room: '',
        class_state: '',
        student_number: '',
        headteacher: '',
        class_start: ''
      },
      searchForm: {
        headteacher: '',
        class_room: '',
        class_state: ''
      }
    }
  },
  methods: {
    changeState(id, state) {
      console.log(id, state)
      changeClazzState({id, state}).then(response => {
        this.$notify.success(response.message)
        this.clazzPageData()
      })
    },
    searchClass() {
      this.currentPage = 1
      this.clazzPageData()
    },
    resetSearchFormData(formName) {
      this.$refs[formName].resetFields();
      this.currentPage = 1
      this.clazzPageData()
    },
    // 编辑
    handleEdit(row) {
      this.title = '编辑'
      this.dialogFormVisible = true
      clazzOne(row.id).then(response => {
        this.form = response.data.clazz
      })

    },
    // 删除
    handleDelete(row) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {   // 确定
        let {id} = row
        // 发送异步请求
        clazzDelete(id).then(response => {
          this.$notify.success(response.message)
          this.clazzPageData()
        })
      })
    },
    // 新增
    add() {
      this.title = '新增'
      this.dialogFormVisible = true
    },
    addOrUpDateClazz() {
      this.$refs.dialogForm.validate(valid => {
        if (valid) {
          clazzAddOrUpdate(this.form).then(response => {
            this.$notify.success(response.message)
            this.dialogFormVisible = false
            this.clazzPageData()
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    handleSizeChange(val) {
      this.pageSize = val
      // 重新获取分页数据
      this.clazzPageData()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.clazzPageData()
    },
    //关闭弹窗时 清除表单数据
    clearInfo() {
      this.form = {
        class_name: '',
        class_room: '',
        class_state: '',
        student_number: '',
        headteacher: '',
        class_start: ''
      }
      this.$refs.dialogForm.clearValidate()
    },
    clazzPageData() {
      clazzPage({
        page: this.currentPage,
        size: this.pageSize,
        headteacher: this.searchForm.headteacher,
        class_room: this.searchForm.class_room,
        class_state: this.searchForm.class_state
      }).then(response => {
        this.total = response.data.total
        this.tableData = response.data.tableData
      })
    }
  },
  created() {
    this.clazzPageData()
  }
}
</script>
<style scoped lang="scss">
#searchForm {
  border-bottom: 1px solid #ccc;
  margin-bottom: 15px;
}
</style>

